import { compressX64 } from "../core/utils/lzstring";
import { sha1 } from "../core/utils/utils";

const compressionPrefix = String.fromCodePoint(1);

self.addEventListener('message', ev => {
  const { data, isCompressed } = ev.data;
  const jsonString = JSON.stringify(data);

  if(isCompressed){
    const checksum = sha1(jsonString);
    let compressed = compressionPrefix + compressX64(checksum + jsonString);
    
    self.postMessage({ data: compressed })
  } else {
    self.postMessage({ data: jsonString })
  }
})